class Solution {

    public static void main(String[] args) {
        ListNode n4 = new ListNode(4);
        ListNode n3 = new ListNode(3, n4);
        ListNode n2 = new ListNode(2, n3);
        ListNode n1 = new ListNode(1, n2);
        swapPairs(n1);
    }

    public static ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        // 创建哨兵位节点
        ListNode newHead = new ListNode(-1);
        // 申请四个结点指针
        ListNode n1 = newHead, n2 = head, n3 = n2.next, n4 = n3.next;
        while (n2 != null && n3 != null) {
            // 修改节点指针指向
            n1.next = n3;
            n3.next = n2;
            n2.next = n4;
            // 更新节点指针
            n1 = n2;
            n2 = n4;
            n3 = n4 == null ? null : n4.next;
            n4 = n3 == null ? null : n3.next;
        }
        return newHead.next;
    }
}


class ListNode {
    int val;
    ListNode next;

    public ListNode() {

    }

    public ListNode(int val) {
        this.val = val;
    }

    public ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}